﻿<UserControl x:Class="CredentialsManager.Views.ChangePasswordView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:p="clr-namespace:CredentialsManager"             
             mc:Ignorable="d"
             Height="250" Width="300">
    <UserControl.Resources>
        <Style x:Key="NoQAndAStyle" TargetType="{x:Type FrameworkElement}">
            <Setter Property="Visibility" Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding RequiresQuestionAndAnswer}" Value="False">
                    <Setter Property="Visibility" Value="Visible" />
                    <Setter Property="Margin" Value="0, 5, 0, 5" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <StackPanel Margin="5">
        <WrapPanel Margin="0, 10, 0, 10">
            <TextBlock>User:</TextBlock>
            <TextBlock Text="{Binding UserName}" />
        </WrapPanel>
        <StackPanel Margin="0, 10, 0, 10">
            <StackPanel.Style>
                <Style TargetType="{x:Type StackPanel}">
                    <Setter Property="Visibility" Value="Collapsed" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RequiresQuestionAndAnswer}" Value="True">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Style>
            <TextBlock Text="{Binding SecurityQuestion}" TextWrapping="WrapWithOverflow" Margin="0, 10, 0, 10" />
            <TextBox Text="{Binding SecurityAnswer, UpdateSourceTrigger=PropertyChanged}" />
        </StackPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="25" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="0" Text="Old password:" 
                       Style="{StaticResource NoQAndAStyle}" />
            <PasswordBox
                Grid.Column="1" Grid.Row="0" 
                p:PasswordBoxHelper.Attach="True"  
                Style="{StaticResource NoQAndAStyle}"
                p:PasswordBoxHelper.Password="{Binding OldPassword, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />                
            <TextBlock Grid.Column="0" Grid.Row="1" Margin="0, 5, 0, 5" Text="New password:" />
            <PasswordBox 
                p:PasswordBoxHelper.Attach="True" 
                p:PasswordBoxHelper.Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                Grid.Column="1" Grid.Row="1" 
                Margin="0, 5, 0, 5" />
            <Rectangle Grid.Row="1" Grid.Column="2" Width="25" Fill="{StaticResource dialogwarning}"
                       Style="{StaticResource CollapseIfIsValidStyle}" DataContext="{Binding PasswordValidity}">
                <Rectangle.ToolTip>
                    <TextBlock Text="{Binding ErrorContent}" />
                </Rectangle.ToolTip>                
            </Rectangle>
            <TextBlock Grid.Column="0" Grid.Row="2" Margin="0, 5, 0, 5" Text="Confirm new password:" />
            <PasswordBox
                p:PasswordBoxHelper.Attach="True" 
                p:PasswordBoxHelper.Password="{Binding CheckPassword, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                Grid.Column="1" Grid.Row="2"
                Margin="0, 5, 0, 5" />
            <Rectangle Grid.Row="2" Grid.Column="2" Width="25" Fill="{StaticResource dialogwarning}"
                       Style="{StaticResource CollapseIfIsValidStyle}"
                       DataContext="{Binding CheckPasswordValidity}">
                <Rectangle.ToolTip>
                    <TextBlock Text="{Binding ErrorContent}" />
                </Rectangle.ToolTip>
            </Rectangle>
        </Grid>
        <WrapPanel>
            <Button Command="{Binding ChangePasswordCommand}" 
                    Content="Change password" 
                    Margin="5" Padding="5" 
                    IsEnabled="{Binding IsValid}" />
            <Button Command="{Binding CloseCommand}" Margin="5" Padding="5">Close</Button>
        </WrapPanel>
    </StackPanel>
</UserControl>
